#include <qDebug>
#include <QFileInfo>
#include <QSettings>
#include "audioclient.h"
#include "logger.h"

audioClient::audioClient()
{
    loadINI();
    pingger.init(localIP);
}

audioClient::~audioClient()
{

}

int audioClient::init()
{
    if(mInited){
        return true;
    }

    print_base_info();
    client.init(localPort, localIP);
    client.connect();
    mInited = true;

    return 0;
}

bool audioClient::is_connected()
{
    return client.isConnected();
}

//字符串以 #开头，支持中文
//例如："#欢迎使用充电机器人请下单"
void audioClient::play(QString txt)
{
    client.sendMsg(txt.toLocal8Bit().data());
}

void audioClient::loadINI()
{
    QFileInfo fileInfo(CONFIG_FILE_PATH);
    if(fileInfo.isFile())
    {
        QSettings setting(CONFIG_FILE_PATH, QSettings::IniFormat);
        setting.setIniCodec("utf-8");

        localIP = setting.value("/AUDIO/serverIP").toString().toStdString();
        localPort  = setting.value("/AUDIO/serverPort").toInt();
    }
}

void audioClient::print_base_info()
{
    LOG(INFO) << "localIP:" << localIP << std::endl;
    LOG(INFO) << "localPort:" << localPort << std::endl;
}
